package demo.test.controller;


import com.entites.DataDeviceSet;
import com.entites.DeviceSet;
import demo.test.service.DataService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;

@RestController
@Slf4j
public class CollectionController {
    @Value("${server.port}")
    private String port;
    Process exe;
    @Resource
    private DataService dataService;


    /**
     * 做一个简单的查询
     * */
    @GetMapping("/get/{id}")
    public DeviceSet  getData(@PathVariable("id") Integer id){

        DeviceSet device=dataService.getDeviceSet(id);
        return device;

    }


    @GetMapping("/getA")
    public List<DeviceSet> getDeviceSets(){

        List<DeviceSet> result = dataService.getDeviceSets();
        log.info(String.valueOf(result));

return result;
    }


    @PostMapping("/getO/{device}")
    public DeviceSet getDeviceSetss(@RequestBody DeviceSet device){

        System.out.println(device.toString());

        return device;
    }


    @GetMapping(value="/get/dataV-t_data_device",produces = "application/json; charset=utf-8")
    public List<DataDeviceSet> get_t_data_device(){
        List<DataDeviceSet> result =dataService.getDataDevice();

        log.info(String.valueOf(result));
        return result;
    }


    @GetMapping ("/testStart")
    public void testStart() throws IOException, InterruptedException {

        Runtime runtime = Runtime.getRuntime();
        this.exe=runtime.exec("java -jar -Dfile.encoding=utf-8 E:\\Demo_ChengSheng\\retrospect-provider.jar");
//        "java -jar -Dfile.encoding=utf-8 -Dserver.port=9004 E:\\Demo_ChengSheng\\retrospect-provider.jar "
        InputStream inputStream = this.exe.getInputStream();

//        System.out.println(exe.pid());
        byte[] bytes = new byte[1024];
        int len = 0;
        while ((len = inputStream.read(bytes)) != 0) {
            System.out.println(new String(bytes,0,len,"GBK"));
        }
//        return ;
    }

    @GetMapping ("/testShut")
    public void testShutdown() throws IOException, InterruptedException {
        this.exe.destroy();
        InputStream inputStream = this.exe.getInputStream();
        byte[] bytes = new byte[1024];
        int len = 0;
        while ((len = inputStream.read(bytes)) != 0) {
            System.out.println(new String(bytes,0,len,"GBK"));
        }
    }

}
